
#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"

struct Rule{
    int a;
    int b;
    Rule(int a,int b):a(a),b(b){

    }
};
class OneLoop{
public:
    std::vector<int> from;
    int max=0;
    void recur(int cnt,int num){
        if(num==0){
            if(cnt>max){
                max=cnt;
            }
            return;
        }
        recur(cnt+1,from[num-1]);
    }
    void run(
            int a
            ){
        for(int i=0;i<a;i++){
            int in;std::cin>>in;
            from.push_back(in);
        }
        for(int i=0;i<from.size();i++){
            recur(1,from[i]);
        }
        printf("%d\n",max);
    }
};
class Solution {
public:
    void run() {

        bool first = true;
        int a,b,c,d,e;
//        std::string s1,s2;
        while (
                std::cin>>a
//                std::getline(std::cin,s1)
                )
        {
//            std::getline(std::cin,s2);
            OneLoop loop;
            loop.run(a);
        }
    }
};

int main() {
//    bool cur_num= true;
    Solution().run();
    return 0;
}

